package com.zxl.manager.dao.impl;

import com.zxl.manager.dao.ProductDao;
import com.zxl.manager.entity.Product;
import com.zxl.manager.entity.ProductDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.*;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by jimmy on 2017/7/9.
 */
@Repository
public class ProductDaoImpl implements ProductDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Product> getAllProd(){
        String sql = "select * from product";
        List<Product> proList = jdbcTemplate.query(sql, new RowMapper(){

            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                Product product = new Product();
                product.setProductId(resultSet.getLong("product_id"));
                product.setProductTypeId(resultSet.getLong("product_type_id"));
                product.setProductName(resultSet.getString("product_name"));
                product.setProductDesc(resultSet.getString("product_desc"));
                product.setProductIcon(resultSet.getString("product_icon"));
                product.setProductDiscountPrice(resultSet.getDouble("product_discount_price"));
                product.setProductPrice(resultSet.getDouble("product_price"));
                product.setProductMinPrice(resultSet.getDouble("product_min_price"));
                product.setCreateTime(resultSet.getDate("create_time"));
                product.setCreateTime(resultSet.getDate("update_time"));
                return product;
            }
        });
        return proList;
    }

    public int insProd(Product product){
        String sql = "insert into product(product_name,product_desc,product_icon,product_discount_price,product_price,product_min_price,product_type_id,product_is_main) value(?,?,?,?,?,?,?,?)";
        int autoIncId = 0;
        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setString(1,product.getProductName());
                ps.setString(2,product.getProductDesc());
                ps.setString(3,product.getProductIcon());
                ps.setDouble(4,product.getProductDiscountPrice());
                ps.setDouble(5,product.getProductPrice());
                ps.setDouble(6,product.getProductMinPrice());
                ps.setLong(7,product.getProductTypeId());
                ps.setString(8,product.getProductMain());
                return ps;
            }
        },keyHolder);
        autoIncId = keyHolder.getKey().intValue();
        return autoIncId;
    }

    public int updProd(Product product){
        String sql = "update product set product_name = ?,product_desc = ?,product_icon = ?,product_discount_price = ?,product_price = ?,product_min_price = ? where product_id = ?";
        Integer res = jdbcTemplate.update(sql,new Object[]{product.getProductName(),product.getProductDesc(),product.getProductIcon(),product.getProductDiscountPrice(),
                product.getProductPrice(),product.getProductMinPrice(),product.getProductId()});
        return res;
    }

    public int delProd(String ids){
        String sql = "delete from product where product_id in ("+ids+")";
        Integer res = jdbcTemplate.update(sql);
        return res;
    }

    public List<ProductDetail> getProdDet(String proId){
        String sql = "select detail_id,product_id,type,value,create_time,update_time from product_detail where product_id = ?";
        List<ProductDetail> delist = jdbcTemplate.query(sql, new Object[]{proId}, new RowMapper<ProductDetail>() {
            @Override
            public ProductDetail mapRow(ResultSet resultSet, int i) throws SQLException {
                ProductDetail pd = new ProductDetail();
                pd.setDetailId(resultSet.getLong("detail_id"));
                pd.setProductId(resultSet.getLong("product_id"));
                pd.setType(resultSet.getString("type"));
                pd.setValue(resultSet.getString("value"));
                pd.setCreateTime(resultSet.getTime("create_time"));
                pd.setUpdateTime(resultSet.getTime("update_time"));
                return pd;
            }
        });
        return delist;
    }

    public int insProdDet(ProductDetail pd){
        String sql = "insert into product_detail(product_id,type,value) values(?,?,?) ";
        Integer res = jdbcTemplate.update(sql,new Object[]{pd.getProductId(),pd.getType(),pd.getValue()});
        return res;
    }

    public int delProdDet(String prodIds){
        String sql = "delete from product_detail where product_id in ("+prodIds+")";
        Integer res = jdbcTemplate.update(sql);
        return res;
    }
}
